﻿#region Apache License
/* 
   Copyright 2011 Jorge Teixeira

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace AdHoc.Wavesque.Test
{
    [TestFixture(Description = "Unit tests for class Sinusoidesque.")]
    public class SinusoidesqueTest
    {      
        [Test(Description = "Create an object representation of a sinusoid.")]
        public void Create()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Step, SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);
            Assert.IsInstanceOf(typeof(Sinusoidesque), f);
        }

        [Test(Description = "Create an object representation of a sinusoid.")]
        public void Create2()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);
            Assert.IsInstanceOf(typeof(Sinusoidesque), f);
        }

        [Test(Description = "Creates a synchronized object representation of a sinusoid.")]
        public void Create3()
        {
            var f = Sinusoidesque.Synchronized(new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1));
            Assert.IsInstanceOf(typeof(Sinusoidesque), f);
            Assert.IsTrue(f.IsSynchronized);
        }

        [Test(Description = "Generate a default number of points of a sinusoid.")]
        public void GetValuesTest()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.GetValues(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a sinusoid.")]
        public void GetValuesTest2()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.GetValues(out x, out y, SinusoidesqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sinusoid.")]
        public void GetValuesTest3()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.GetValues(SinusoidesqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(SinusoidesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sinusoid.")]
        public void GetValuesTest4()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.GetValues(SinusoidesqueTestCaseData.NumberOfPoints, out x, out y, SinusoidesqueTestCaseData.X0);

            Assert.AreEqual(SinusoidesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sinusoid.")]
        public void GetValuesTest5()
        {
            var f = Sinusoidesque.Synchronized(new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1));

            Assert.IsTrue(f.IsSynchronized);

            double[] x;
            double[] y;
            f.GetValues(SinusoidesqueTestCaseData.NumberOfPoints, out x, out y, SinusoidesqueTestCaseData.X0);

            Assert.AreEqual(SinusoidesqueTestCaseData.NumberOfPoints, y.Length);
        }
     
        [Test(Description = "Generate a given number of points of a sinusoid."), TestCaseSource(typeof(SinusoidesqueTestCaseData), "Data")]
        public void GetValuesTest(double step, double amplitude, double frequency, double phase, double x0, uint numberOfPoints)
        {
            double[] aX;
            double[] aY;

            var f = new Sinusoidesque(step, amplitude, frequency, phase);
            f.GetValues(numberOfPoints, out aX, out aY, x0);
            
            Assert.AreEqual(numberOfPoints, aY.Length);
            
            List<double> bX = new List<double>();
            List<double> bY = new List<double>();
                        
            foreach(double pos in aX)
            {
            	double[] x;
            	double[] y;
            	var g = new Sinusoidesque(step, amplitude, frequency, phase);
            	g.GetValues(1, out x, out y, pos);
            	
            	Assert.AreEqual(1, x.Length);
            	Assert.AreEqual(1, y.Length);
            	
            	bX.AddRange(x);
            	bY.AddRange(y);
            }
            
            Assert.AreEqual(aX, bX);
            Assert.AreEqual(aY, bY);
        }

        [Test(Description = "Generate a given number of points of a sinusoid, half wave rectification.")]
        public void ToHalfWaveRectification()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(SinusoidesqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(SinusoidesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sinusoid, half wave rectification.")]
        public void ToHalfWaveRectification2()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(SinusoidesqueTestCaseData.NumberOfPoints, out x, out y, SinusoidesqueTestCaseData.X0);

            Assert.AreEqual(SinusoidesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a default number of points of a sinusoid, half wave rectification.")]
        public void ToHalfWaveRectification3()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a sinusoid, half wave rectification.")]
        public void ToHalfWaveRectification4()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(out x, out y, SinusoidesqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sinusoid, full wave rectification.")]
        public void ToFullWaveRectification()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(SinusoidesqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(SinusoidesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sinusoid, full wave rectification.")]
        public void ToFullWaveRectification2()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(SinusoidesqueTestCaseData.NumberOfPoints, out x, out y, SinusoidesqueTestCaseData.X0);

            Assert.AreEqual(SinusoidesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sinusoid, full wave rectification.")]
        public void ToFullWaveRectification3()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a sinusoid, full wave rectification.")]
        public void ToFullWaveRectification4()
        {
            var f = new Sinusoidesque(SinusoidesqueTestCaseData.Amplitude, SinusoidesqueTestCaseData.Frequency, SinusoidesqueTestCaseData.Phase1);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(out x, out y, SinusoidesqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }
    }
}
